// pages/volunteer/loading/idnex.js
const {
  viluntarySmartAdd
} = require('../../../api/ai')
var timer = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timer: null,
    percentage: 0,
    content: '正在解读中',
    flag:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.params) {
      const params = JSON.parse(decodeURIComponent(options.params))
      this.setData({
        params
      })
      this.volunteerAdd(params)
    }
    this.timer()
  },
  async volunteerAdd(params) {
    var {
      result
    } = await viluntarySmartAdd(params)
    if (result) {
      result.level1Name = result.localBatchName == '本科批' ? '本科' : '专科（高职）'
      this.setData({
        flag:true,
        newParams:result
      })
      // wx.redirectTo({
      //   url: '/pages/volunteer/recommend/index?params=' + encodeURIComponent(JSON.stringify(params)),
      // })
    }
  },
  onUnload() {
    clearInterval(timer)
  },
  timer() {
    var {
      percentage,
    } = this.data

    const _this = this
    timer = setInterval(function () {
      percentage = percentage>40?_this.data.flag?percentage + 1:percentage:percentage+1
      _this.setData({
        percentage
      })
      if (percentage == 20) {
        _this.setData({
          content: '正在生成中'
        })
      }
      if (percentage == 40) {
        _this.setData({
          content: '确定成绩意向中'
        })
      }
      if (percentage == 50) {
        _this.setData({
          content: '复合意向志愿生成中'
        })
      }
      if (percentage == 70) {
        _this.setData({
          content: '志愿完整性检查中'
        })
      }
      if (percentage == 90) {
        _this.setData({
          content: '志愿规则检查中'
        })
      }

      if (percentage >= 100&&_this.data.flag) {
        _this.setData({
          percentage: percentage,
          content: '解读完成'
        })
        setTimeout(() => {
          wx.redirectTo({
            url: '/pages/volunteer/recommend/index?params=' + encodeURIComponent(JSON.stringify(_this.data.newParams)),
          })
        }, 500);
        clearInterval(timer)
      }
    }, 50)

  },


})